var Request = require('../request')
const $request = new Request()
const config = require('../../config')
var commonModule = require('./common');
var common = new commonModule();

class analyseManageModule {
    /**
     * 功能描述
     * @param {*} params
     * @param {*} res
     * @param {*} req
     */
    getCategoryTree(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/getCategoryTree'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    // 保存单个图形
    saveChartComponent(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/saveChartComponent'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    } 
    
    // 删除单个图形指标
    delChartComponent(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/delChartComponent'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }   

    // 获取图形指标列表
    getChartComponent(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getChartComponent'
        params.page = params.data.page;
        params.size = params.data.size;
        delete params.data.page;
        delete params.data.size;
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }     

    getDefaultCategoryTree(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/getDefaultCategoryTree'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getCustomerCategoryTree(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/getCustomerCategoryTree'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    createAnalyseCategory(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/createAnalyseCategory'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    updateAnalyseCategory(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/updateAnalyseCategory'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    delAnalyseCategory(params, res, req) {
        params.data = params.data.id;
        // console.log(params.data)
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/delAnalyseCategory'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    batchDelAnalyseCategories(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/batchDelAnalyseCategories'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    initTenantAnalyse(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/category/initTenantAnalyse'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getChildAnalysePageList(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getChildAnalysePageList'
        params.page = params.data.page;
        params.size = params.data.size;
        delete params.data.page;
        delete params.data.size;
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    createAnalysePage(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/createAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    updateAnalysePage(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/updateAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    delAnalysePage(params, res, req) {
        params.data = params.data.id;
        // console.log(params.data)
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/delAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAnalysePage(params, res, req) {
        params.data = params.data.id;
        // console.log(params.data)
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAnalysePageConfig(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/getAnalysePageConfig'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getPublicAnalysePageConfig(params, res, req) {
      let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/getAnalysePageConfig'
      params.data.tenantCode && delete params.data.tenantCode;
      $request.handleRequest(
        {
          method: 'post',
          params: params,
          actionUrl: apiUrl,
        },
        res
      )
    }

    createAnalysePageConfig(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/createAnalysePageConfig'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    publishAnalysePageConfig(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/publishAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAnalysePageConfigList(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/getAnalysePageConfigList'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    delAnalysePageConfig(params, res, req) {
        params.data = params.data.id;
        // console.log(params.data)
        let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/delAnalysePageConfig'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    updateAnalysePageConfig(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/pageConfig/updateAnalysePageConfig'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAllTable(params, res, req) {
        params.data = params.data.roleSign;
        let apiUrl = config.serverUrl_BI + '/ui/analyse/model/getAllTable'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAllColumns(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/db/getAllColumns'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getDataTree(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/model/getDataTree'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    saveDataTree(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/model/saveDataTree'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getAnalysePageDrafts(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getAnalysePageDrafts'
        params.page = params.data.page;
        params.size = params.data.size;
        delete params.data.page;
        delete params.data.size;
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    batchDelAnalysePage(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/batchDelAnalysePage'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    getComponentData(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/model/getComponentData'
        $request.handleRequest(
            {
                method: 'post',
                params: params,
                actionUrl: apiUrl,
            },
            res
        )
    }

    //  不需要用户登录
    getPublicComponentData(params, res, req) {
      let apiUrl = config.serverUrl_BI + '/ui/analyse/model/getComponentData'
      params.data.tenantCode && delete params.data.tenantCode;
      $request.handleRequest(
        {
          method: 'post',
          params: params,
          actionUrl: apiUrl,
        },
        res
      )
    }

    updateShareConfig(params, res, req) {
      let apiUrl = config.serverUrl_BI + '/biUiAnalysePublicShare/update'
      $request.handleRequest(
        {
          method: 'post',
          params: params,
          actionUrl: apiUrl,
        },
        res
      )
    }

    getShareConfig(params, res, req) {
      let apiUrl = config.serverUrl_BI + '/biUiAnalysePublicShare/get'
      params.data = params.data.pageId;
      $request.handleRequest(
        {
          method: 'post',
          params: params,
          actionUrl: apiUrl,
        },
        res
      )
    }

    getShareUrl(params, res, req) {
        let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getUrl'
        params.data = {pageId: params.data.pageId};
        $request.handleRequest(
          {
            method: 'post',
            params: params,
            actionUrl: apiUrl,
          },
          res
        )
      }

  getReportInfo(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/biUiAnalysePublicShare/decrypt'
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  getPublicReportInfo(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/biUiAnalysePublicShare/decrypt'
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  validateReportPwd(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/biUiAnalysePublicShare/password/validate'
    params.data.tenantCode && delete params.data.tenantCode;
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  //设置主页
  setHomePage(params, res, req) {
    params.data = params.data.data;
    let apiUrl = config.serverUrl_BI + '/ui/analyse/page/setHomePage';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  //获取主页的ID
  getHomePageId(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/page/getHomePageId';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 权限树信息
  selectOrgPosWithEmpTree(params, res, req) {
    let apiUrl = config.folderAuthUrl + '/fndPortalOrganization/selectOrgPosWithEmpTree';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

   // 设置文件夹权限
  saveResourcePermission(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/resource/saveResourcePermission';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 查询订阅配置
  subscribeGet(params, res, req) {
    params.data = params.data.pageId;
    let apiUrl = config.serverUrl_BI + '/subscribe/get';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 订阅
  subscribeSave(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/subscribe/save';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 查询执行记录
  subscribeLog(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/subscribe/getExecuteLog';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 查询资源权限
  getResourcePermission(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/resource/getResourcePermission';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  //  文本组件获取德勤方案级联数据
  async getInternalCategoryTree(params, res){
    params.data.userId = params.operator;
    //	获取主系统顶部模块id
    const topMenusInfo = await common.getTopMenu( params ).catch( err => err );

    let systemId = '';
    //	找到bi模块的id
    if ( topMenusInfo.success ) {
      const biItem = topMenusInfo.data.find( v => v.permittedAction === 'newbi' );
      if ( biItem ) {
        systemId = biItem.systemId;
      }
    }

    if ( !systemId ) return res.json( { status: -1, err: topMenusInfo.message } );

    let menu = '';
    //	获取bi模块的菜单
    params.data.systemId = systemId;
    const leftMenuInfo = await common.getTopMenu( params ).catch( err => err );

    if ( leftMenuInfo.success ) {
      menu = leftMenuInfo.data[0].childrenList;
    }

    if( !menu) return res.json( { status: -1, err: leftMenuInfo.message } );
    console.log(menu);

    //  获取德勤方案下的菜单,deloitteScheme在admin后台中定义
    const deloitteScheme = menu.find( v => v.permittedAction === 'deloitteScheme' );
    if(!deloitteScheme ) return res.json( { status: 1, data: [] } );

    let resData = [];
    let promiseArray = [];
    deloitteScheme.childrenList.forEach(v => {
      let p = this.getReportList( params, v.resourceId ).then(children => {
        v.children = children;
        return v;
      });
      promiseArray.push(p);
    });
    Promise.all(promiseArray)
           .then( data => res.json( { status: 1, data: data } ))
           .catch( err => res.json( { status: -1, err: err } ));
  }

  //  获取德勤方案下的文件夹的数据表
  getReportList( params, resourceId ) {
    params.data = {
      categoryId: resourceId, name: ''
    };
    params.page = 1;
    params.size = 0;
    return new Promise( ( resolve, reject ) => {
      $request.doAction( {
        method: 'post',
        params: params,
        actionUrl: config.serverUrl_BI + '/ui/analyse/page/getChildAnalysePageList',
      } ).then( ( body ) => {
        resolve( body.data.rows );
      } ).catch( ( err ) => {
        reject( err );
      } );
    } )
  }

  // 保存或者更新层级
  saveOrUpdateFolder(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/model/saveOrUpdateFolder';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 获取发布信息
  getDataPermission(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/resource/getDataPermission';
    params.data = params.data.pageId
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 通过code获取数据集
  getDataSetByCode(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/biDataSet/getDataSetByCode';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }
  
  // 查询报表权限
  getPagePermissionByCode(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/resource/getPagePermissionByCode';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 替换数据集
  replaceDataSet(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/page/replace';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 复制德勤方案
  copyDeloittePage(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/page/copyDeloittePage';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }

  // 根据报表code、文件夹id查询报表权限
  getPageDataPermissionByCode(params, res, req) {
    let apiUrl = config.serverUrl_BI + '/ui/analyse/resource/getPageDataPermissionByCode';
    $request.handleRequest(
      {
        method: 'post',
        params: params,
        actionUrl: apiUrl,
      },
      res
    )
  }
}

module.exports = analyseManageModule;
